package com.cavy.platform.server.validator;

import com.cavy.platform.server.BeansProvider;
import com.cavy.platform.server.repository.WorkSpaceRepository;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 验证项目名称不能重复
 */
public class WorkspaceExistValidator implements ConstraintValidator<WorkspaceExist, String> {
    private WorkSpaceRepository bean;

    @Override
    public void initialize(WorkspaceExist notRepeatPluginName) {
        bean = BeansProvider.getBean(WorkSpaceRepository.class);
    }

    @Override
    public boolean isValid(String name, ConstraintValidatorContext context) {
        int count = bean.countByName(name);
        return count > 0;
    }
}
